package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.PrdSoltRuleDao;
import com.fdb.efp.nls.service.domain.PrdSoltRule;
import com.fdb.efp.nls.service.facade.PrdSoltRuleService;
import com.fdb.efp.nls.service.vo.PrdSoltRuleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 产品规则插槽参数
 * 
 * @author huangwb
 * @since 2018年12月26日 下午7:49:39
 * @version 1.0
 */
@Service("prdSoltRuleService")
public class PrdSoltRuleServiceImpl implements PrdSoltRuleService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(PrdSoltRuleServiceImpl.class);

	@Autowired
	private PrdSoltRuleDao prdSoltRuleDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertPrdSoltRule(PrdSoltRuleVO inPrdSoltRuleVo) {
		logger.debug("当前新增数据为:" + inPrdSoltRuleVo.toString());
		int num = 0;
		try {
			PrdSoltRule prdSoltRule = new PrdSoltRule();
			beanCopy(inPrdSoltRuleVo, prdSoltRule);
			num = prdSoltRuleDao.insertPrdSoltRule(prdSoltRule);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(PrdSoltRuleVO inPrdSoltRuleVo) {
		logger.debug("当前删除数据条件为:" + inPrdSoltRuleVo);
		int num = 0;
		try {
			PrdSoltRule prdSoltRule = new PrdSoltRule();
			beanCopy(inPrdSoltRuleVo, prdSoltRule);
			num = prdSoltRuleDao.deleteByPk(prdSoltRule);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inPrdSoltRuleVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(PrdSoltRuleVO inPrdSoltRuleVo) {
		logger.debug("当前修改数据为:" + inPrdSoltRuleVo.toString());
		int num = 0;
		try {
			PrdSoltRule prdSoltRule = new PrdSoltRule();
			beanCopy(inPrdSoltRuleVo, prdSoltRule);
			num = prdSoltRuleDao.updateByPk(prdSoltRule);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inPrdSoltRuleVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public PrdSoltRuleVO queryByPk(PrdSoltRuleVO inPrdSoltRuleVo) {

		logger.debug("当前查询参数信息为:" + inPrdSoltRuleVo);
		try {
			PrdSoltRule queryPrdSoltRule = new PrdSoltRule();
			beanCopy(inPrdSoltRuleVo, queryPrdSoltRule);
			PrdSoltRule queryRslPrdSoltRule = prdSoltRuleDao.queryByPk(queryPrdSoltRule);
			if (Objects.nonNull(queryRslPrdSoltRule)) {
				PrdSoltRuleVO outPrdSoltRuleVo = beanCopy(queryRslPrdSoltRule, new PrdSoltRuleVO());
				logger.debug("当前查询结果为:" + outPrdSoltRuleVo.toString());
				return outPrdSoltRuleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<PrdSoltRuleVO> queryAllOwner(PrdSoltRuleVO prdSoltRuleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PrdSoltRuleVO> list = null;
		try {
			List<PrdSoltRule> prdSoltRules = prdSoltRuleDao.queryAllOwnerByPage(prdSoltRuleVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + prdSoltRules.size());
			pageSet(prdSoltRules, prdSoltRuleVo);
			list = (List<PrdSoltRuleVO>) beansCopy(prdSoltRules, PrdSoltRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<PrdSoltRuleVO> queryAllCurrOrg(PrdSoltRuleVO prdSoltRuleVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<PrdSoltRule> prdSoltRules = prdSoltRuleDao.queryAllCurrOrgByPage(prdSoltRuleVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + prdSoltRules.size());
		List<PrdSoltRuleVO> list = null;
		try {
			pageSet(prdSoltRules, prdSoltRuleVo);
			list = (List<PrdSoltRuleVO>) beansCopy(prdSoltRules, PrdSoltRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<PrdSoltRuleVO> queryAllCurrDownOrg(PrdSoltRuleVO prdSoltRuleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<PrdSoltRule> prdSoltRules = prdSoltRuleDao.queryAllCurrDownOrgByPage(prdSoltRuleVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + prdSoltRules.size());
		List<PrdSoltRuleVO> list = null;
		try {
			pageSet(prdSoltRules, prdSoltRuleVo);
			list = (List<PrdSoltRuleVO>) beansCopy(prdSoltRules, PrdSoltRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * @description:根据产品Id和插槽代码查询要执行的规则
	 * @param prdId
	 * @param soltCode
	 * @return
	 * @date:2018年12月29日 上午9:56:25
	 * @author:hengyh
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<PrdSoltRuleVO> queryByPrdIdAndSoltCode(String prdId, String soltCode) {
		logger.debug("当前查询参数信息为, 产品Id: " + prdId + ", 插槽代码：" + soltCode);
		List<PrdSoltRule> prdSoltRules = prdSoltRuleDao.queryByPrdIdAndSoltCode(prdId, soltCode);
		List<PrdSoltRuleVO> list = null;
		try {
			list = (List<PrdSoltRuleVO>) beansCopy(prdSoltRules, PrdSoltRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PrdSoltRuleVO> queryPrdSoltRuleList(PrdSoltRuleVO prdSoltRuleVo) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PrdSoltRuleVO> list = null;
		try {
			List<PrdSoltRule> prdSoltRules = prdSoltRuleDao.queryPrdSoltRuleByPage(prdSoltRuleVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + prdSoltRules.size());
			pageSet(prdSoltRules, prdSoltRuleVo);
			list = (List<PrdSoltRuleVO>) beansCopy(prdSoltRules, PrdSoltRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}
	
}
